// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_BPT_TASK
// * CreateDate    : 2013-08-28 11:00:55
// * Creater       : CodeSmith
// * LastChangeDate: 2013-08-28 11:00:55
// * LastChanger   : 2013-08-28 11:00:55
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Bpt
{
/// <summary>
    /// BptTask
    /// </summary>
    public partial class BptTaskBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objBptTaskEntity">Entity</param>
        /// <returns>Entity</returns>
        public static BptTaskEntity GetBptTask(BptTaskEntity objBptTaskEntity)
        {
            return DataAccess.SelectSingle<BptTaskEntity>(objBptTaskEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objBptTaskEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertBptTask(BptTaskEntity objBptTaskEntity)
        {
            return DataAccess.Insert(objBptTaskEntity);
        }

        public static bool InsertBptTask(List<BptTaskEntity> objBptTaskEntityList)
        {
            return DataAccess.Insert(objBptTaskEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objBptTaskEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateBptTask(BptTaskEntity objBptTaskEntity)
        {
            return DataAccess.Update(objBptTaskEntity);
        }

        public static bool UpdateBptTask(List<BptTaskEntity> objBptTaskEntityList)
        {
            return DataAccess.Update<BptTaskEntity>(objBptTaskEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objBptTaskEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteBptTask(BptTaskEntity objBptTaskEntity)
        {
            return DataAccess.Delete(objBptTaskEntity);
        }

        public static bool DeleteBptTask(List<BptTaskEntity> objBptTaskEntityList)
        {
            return DataAccess.Delete<BptTaskEntity>(objBptTaskEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetBptTaskAll()
        {
            return DataAccess.SelectAllDataSet<BptTaskEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<BptTaskEntity> GetBptTaskAllList()
        {
            return DataAccess.SelectAll<BptTaskEntity>();
        }
        #endregion


        #region Functions
        public static DataSet GetBptTaskByCondition(int pageIndex, int pageCount, string condition)
        {
            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from bpt_task) as cnt
from bpt_task
where id not in (
    select top ({1}*({2}-1)) id from bpt_task order by created_date
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageCount, pageCount, pageIndex);
                sql = @"select bt.*,ISNULL(ba.APP_CODE,BT.BPT_APP_ID) APP_CODE ,ISNULL(sapp.APP_NO,C.PLAN_ORDER_ID) as SALES_APP_NO,
mc.CUSTOMER_NAME,mp.PRODUCT_NAME+'('+mp.PRODUCT_CODE+')' as PRODUCT,ba.CONTRACT_MODEL,ba.MATERIAL 
from ({0}) bt
left join BPT_APP ba on bt.BPT_APP_ID=ba.ID
left join SALES_APP sapp on ba.SALES_APP_ID=sapp.ID
left join MST_CUSTOMER mc on sapp.CUSTOMER_ID=mc.ID
left join MST_PRODUCT mp on bt.PRODUCT_ID=mp.PRODUCT_ID 
LEFT JOIN BPT_BLUEPRINT_CHANGES C ON BT.BPT_APP_ID=C.CHANGES_CODE";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date
) order by created_date desc";
                string sqlSource = @"select bt.*,ISNULL(ba.APP_CODE,BT.BPT_APP_ID) APP_CODE ,ISNULL(sapp.APP_NO,C.PLAN_ORDER_ID) as SALES_APP_NO,
mc.CUSTOMER_NAME,mp.PRODUCT_NAME+'('+mp.PRODUCT_CODE+')' as PRODUCT,ba.CONTRACT_MODEL,ba.MATERIAL 
from BPT_TASK bt
left join BPT_APP ba on bt.BPT_APP_ID=ba.ID
left join SALES_APP sapp on ba.SALES_APP_ID=sapp.ID
left join MST_CUSTOMER mc on sapp.CUSTOMER_ID=mc.ID
left join MST_PRODUCT mp on bt.PRODUCT_ID=mp.PRODUCT_ID
LEFT JOIN BPT_BLUEPRINT_CHANGES C ON BT.BPT_APP_ID=C.CHANGES_CODE
WHERE 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageCount, pageCount, pageIndex, sqlSource);
            }
            return DataAccess.SelectDataSet(sql);
        }

        public static bool SaveTask(BptTaskEntity bptTask,List<BptBlueprintEntity> bps)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Update(bptTask, broker);
                    DataAccess.Insert(bps, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }

        public static bool ApproveTask(BptTaskEntity bptTask, SalesAppItemEntity salesAppItem,List<MstProductEntity> products, ProdPlanOrderEntity prodPo)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Update(bptTask, broker);
                    DataAccess.Update(salesAppItem, broker);
                    DataAccess.Update(products, broker);
                    DataAccess.Insert(prodPo, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }
                
        #endregion
    }
	
	
}	

